package com.jketing.controller.ams.base;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jketing.controller.BaseController;
import com.jketing.entity.ams.base.Region;
import com.jketing.service.ams.base.RegionService;

/**
 * Controller 请求处理
 * 
 * @author  李荣华
 * @version v1.0
 * @date    2017-11-18
 * @since   Spring-mvc 4.3.8
 */
@Controller
@RequestMapping("/region")
public class RegionController extends BaseController<Region,String>{

	/**
	 *  service 	服务层自动注入
	 */
	private RegionService service;
	/**
	 *  service 自动注入服务层操作实例
	 */
	@Autowired
	public void setService(RegionService service) {
		super.setBaseService(service);
		this.service = service;
	}
	
	@Override
	protected String getPrefixPath() {
		return "base/region/";
	}

	@Override
	protected String getQueryMode() {
		return "Entity";
	}
	
	/**
	 * 初始化区域单选对话框
	 */
	@Override
	protected void initSingleSelect(Map<String, Object> map) {
		List<Region> list = service.getRegionList(2);//加载省级列表
		map.put("list", list);
		map.put("topTree",service.getTree(service.getRootId()).toString());
	}

	/**
	 * 初始化区域多选对话框
	 */
	@Override
	protected void initMultiSelect(Map<String, Object> map) {
		List<Region> list = service.getRegionList(2);//加载省级列表
		map.put("list", list);
		map.put("topTree",service.getTree(service.getRootId()).toString());
	}
	
	/**
	 * 初始化区域列表框架页
	 */
	@Override
	protected void initListFrame(Map<String, Object> map) {
		List<Region> list = service.getRegionList(2);//加载省级列表
		map.put("list", list);
		map.put("topTree", service.getTree((String)map.get("rootId")));//加载从id为1的根节点开始
	}
	
	@Override
	protected Region getQuery(Region query) {
		String type = (String)getSession().getAttribute("region_type");
		if("redirect".equals(type)) {
			request.setAttribute("type", type);
			if("Entity".equals(getQueryMode())) {
				query = (Region)getSession().getAttribute("region_query");
			}
		}
		//可在此处增加修改查询条件的代码
		return query;
	}
	
	/**
	 * 异步请求树型子节点，返回json数据
	 * 
	 * xml配置了mvc:message-converters，全局编码统一转换，则不需要produces
	 * 
	 * @return 无返回，直接向response里写数据
	 * @throws IOException 
	 */
	@RequestMapping(value="/getTree",method=RequestMethod.GET,produces="application/json; charset=utf-8")
	@ResponseBody
	public String getTree(String id){
		return service.getTree(id).toString();
	}
	
	/**
	 * 异步请求树型子节点，返回json数据
	 * 
	 * xml配置了mvc:message-converters，全局编码统一转换，则不需要produces
	 * 
	 * @return 无返回，直接向response里写数据
	 * @throws IOException 
	 */
	@RequestMapping(value="/getSubTree",method=RequestMethod.POST,produces="application/json; charset=utf-8")
	@ResponseBody
	public String getSubTree(String id){
		return service.getSubTree(id).toString();
	}
	
	/**
	 *  初始化新增编辑页
	 */
	@Override
	protected void initAdd(Map<String, Object> map) {
		String order = (String)request.getParameter("order");
		String parentId = (String)request.getParameter("parentId");
		Region parent = new Region();
		parent.setId(parentId);
		Region entity = new Region();
		entity.setParent(parent);
		map.put("entity", entity);
		List<Region> list = new ArrayList<Region>();
		list.add(service.getOne(parentId));
		map.put("list", list);
		//新增后返回所选树型节点列表
		getSession().setAttribute("region_type", "redirect");
		getSession().setAttribute("region_query", entity);
		getSession().setAttribute("region_order", order);
	}
	
	/**
	 *  初始化更新编辑页
	 */
	@Override
	protected void initEdit(Map<String, Object> map) {
		Region entity = (Region)map.get("entity");
		List<Region> list = new ArrayList<Region>();
		list.add(entity.getParent());
		map.put("list", list);
	}
}
